
/**
 * modify to String object
 */
// replace one character to a new substring
String.prototype.SingleReplace = function(single,newSub){
	var i = 0;
	var result = "";
	for(i=0;i<this.length;i++){
		var ch = this.charAt(i);
		if(ch == single){
			result = result + newSub;
		}else{
			result = result + ch;
		}
	}
	return result;
}

/**
 * add function to document
 */
document.allElements = [];

function addChildElementToDocument(element) {
	var child = element.childNodes;
	var i = 0;

	// add childs
	for (i = 0; i < child.length; i++) {
		var e = child[i];
		document.allElements[document.allElements.length] = e;
	}

	// search the sub childs
	for (i = 0; i < child.length; i++) {
		addChildElementToDocument(child[i]);
	}
}

document.getElementsByClassName = function(className){
	var result = [];
	var i = 0;
	for(i=0;i<document.allElements.length;i++){
		if(!document.allElements[i].className) continue;
		
		var array_className = document.allElements[i].className.split(" ");
		
		var j = 0;
		for(j=0;j<array_className.length;j++){
			if(array_className[j] == className){
				result[result.length] = document.allElements[i];
			}
		}
		
	}
	return result;
}


/**
 * Common, contains some common functions
 */
var Common = function(){
	addChildElementToDocument(document);
}




/**
 * object function
 */

// show hidden out, by converting unicode to string
Common.prototype.convertUnicodeClassElementToTargetElement = function(){
	var list = document.getElementsByClassName("unicode");
	var i=0;
	for(i=0;i<list.length;i++){
		document.getElementById(this.getTargetID(list[i].id)).value = Common.getStringFromUnicode(list[i].value);
	}
}
	
Common.prototype.convertStringClassElementToTargetElement = function(){
	var list = document.getElementsByClassName("string");
	var i=0;
	for(i=0;i<list.length;i++){
		document.getElementById(this.getTargetID(list[i].id)).value = Common.getUnicodeFromString(list[i].value);
	}
}

// escape the string, from unsafe string to safe string by converting '<' to &#60; 
// select all the input/textarea(id) with the class name "string" to element which located with the id.safeString
Common.prototype.convertStringClassElementToSafeStringIdElement = function(){
	var list = document.getElementsByClassName("string");
	var i=0;
	for(i=0;i<list.length;i++){
		document.getElementById(this.getSafeStringID(list[i].id)).innerHTML = list[i].value.SingleReplace("<","&#60;");
	}
}



// convert all input value from string to unicode, with class name string
Common.prototype.convertCommUnicodeClassInputValueFromStringToUnicode = function(){
	var list = document.getElementsByClassName("commUnicode");
	var i=0;
	for(i=0;i<list.length;i++){
		list[i].value = Common.getUnicodeFromString(list[i].value);
	}
}



// convert all element with className "safeString" from unicode to safe String
Common.prototype.convertSafeStringClassElementInnerHtmlFromUnicodeToSafeString = function(){
	var list = document.getElementsByClassName("safeString");
	var i=0;
	for(i=0;i<list.length;i++){
		list[i].innerHTML = Common.safeString(Common.getStringFromUnicode(list[i].innerHTML));
	}
}

/**
 * class function
 * -------------------------------
 */

// safe string
Common.safeString = function(str){
	var i = 0;
	var result = "";
	for(i=0;i<str.length;i++){
		var ch = str.charAt(i);
		if(ch == '<'){
			result = result + "&#60;";
		}else{
			result = result + ch;
		}
	}
	return result;
}





//convert the text into unicode, 4 digits for one single character
Common.getUnicodeFromString = function(str){
	var result="";
	for(i=0;i<str.length;i++){
		var unicode = "0000" + str.charCodeAt(i).toString(16).toUpperCase();
		unicode = unicode.substring(unicode.length-4);
		result = result + unicode;			
	}
	return result;
}

// convert a sequence of unicode(hex) to a string
Common.getStringFromUnicode = function(unicode){
	var index=0;
	var unit = unicode.substr(index*4,4);
	var result = "";
	while(unit.length==4){
		result = result + String.fromCharCode(parseInt(unit, 16));
		index++;
		unit = unicode.substr(index*4,4);
	}
	return result;
}